package com.example.service.webhook.business.bank.repository;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.example.service.webhook.business.bank.entity.BankBillEntity;
import com.example.service.webhook.repository.base.BaseDAO;
import com.example.service.webhook.util.DateUtil;
/**
 * 网银账单处理类
 * 这里用的是jdbctemplate，DAO层可以根据自己的业务来选择
 * ClassName: BankBillRepository    
 * date: 2016年7月19日 下午6:29:23   
 * @author yuandong  
 * @version   
 * @since JDK 1.6
 */
@Repository
public class BankBillRepository extends BaseDAO<BankBillEntity>{
	
	@Resource(name="templateBank")
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
    
    public void saveBill(BankBillEntity entity) throws Exception  {
    	entity.setCreateTime(DateUtil.getCurrentDate());
    	entity.setLastModifyTime(DateUtil.getCurrentDate());
		this.add(entity);
    }
    
    
    public void updateBill(BankBillEntity entity) throws Exception  {
    	entity.setLastModifyTime(DateUtil.getCurrentDate());
		this.modify(entity);
    }
    
    /**
     * 
     * getTBillById:查询一条账单. <br/>  
     * 这里是以userId+cardId+billId作为唯一条件<br/>  
     * 需要按照自己的业务进行调整.<br/>  
     * @author yuandong  
     * @param userId
     * @param cardId
     * @param billId
     * @return
     * @throws Exception  
     * @since JDK 1.6
     */
    public BankBillEntity getTBillById(String userId,String cardId,String billId) throws Exception {
        String sql = "";
        String tableName = "t_bankbill";
        sql = "select * from " + tableName + " where userId=? and cardId=? and billId=?";
        BankBillEntity pojo = null;
        try{
         pojo = (BankBillEntity) queryOne(sql, new Object[] {userId,cardId, billId }, new BankBillEntity());	
        }catch(EmptyResultDataAccessException e){
        	return null;
        }
        
        return pojo;
    }
    
    
    public BankBillEntity getTBillByBillMonth(String userId,String cardId,String billMonth) throws Exception {
        String sql = "";
        String tableName = "t_bankbill";
        sql = "select * from " + tableName + " where userId=? and cardId=? and billMonth=?";
        BankBillEntity pojo = null;
        try{
         pojo = (BankBillEntity) queryOne(sql, new Object[] {userId,cardId, billMonth }, new BankBillEntity());	
        }catch(EmptyResultDataAccessException e){
        	return null;
        }
        
        return pojo;
    }
    
    
}
